package com.springmvc.platform.shiro.realm;


import com.springmvc.platform.base.shiro.LoginUser;
import com.springmvc.platform.manage.domain.SysUser;
import com.springmvc.platform.manage.service.SysUserService;
import com.springmvc.platform.util.BeanUtil;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author zhouhong
 * @create 2017/9/16
 * @modify
 */
public class LoginRealm extends AuthorizingRealm {
    @Autowired
    private SysUserService sysUserService;
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获取登录账号密码
        UsernamePasswordToken token=(UsernamePasswordToken)authenticationToken;
        //获取登录账号
        String loginName=token.getUsername();
        //根据输入账号查询时候存在用户
        SysUser sysUser=sysUserService.queryUserByLoginName(loginName);
        //用户不存在
        if(BeanUtil.isEmpty(sysUser)){
            throw new UnknownAccountException("用户名不存在");
        }
        //设置登录信息
//        Md5Hash toMD5=new Md5Hash("1",null,10);
//        System.out.println(toMD5.toString());
        LoginUser loginUser=new LoginUser();
        loginUser.setLoginName(loginName);
        loginUser.setUserId(sysUser.getId());
        loginUser.setUserCode(sysUser.getUserCode());
        return new SimpleAuthenticationInfo(loginUser,sysUser.getPassword(),"");
    }
}
